package org.example.task;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TimeTask {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 打印当前时间
     */
    public void printCurrentTime() {
        String currentTime = dateFormat.format(new Date());
        // 简化输出，只保留时间信息
        System.out.println("当前时间: " + currentTime);
    }

    /**
     * 倒计时功能
     * @param seconds 倒计时秒数
     */
    public void countdown(int seconds) {
        System.out.println("开始倒计时 " + seconds + " 秒...");
        for (int i = seconds; i > 0; i--) {
            // 简化输出，只保留倒计时信息
            System.out.println("倒计时: " + i + " 秒");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.out.println("倒计时任务被中断");
                return;
            }
        }
        System.out.println("倒计时结束!");
    }
}
